package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.constraint.Constraints;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.SalesVolumeEntity;

/**
 * Created by qiuzhenhuan on 2019/3/25.
 */

public class ADA_SalesVolume extends RecyclerView.Adapter<ADA_SalesVolume.ViewHolder> {


    private Context mContext;

    private List<SalesVolumeEntity> mData;

    private boolean isIntegralGoods = false;

    public ADA_SalesVolume(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sales_volume_list, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(mContext, 50));
        view.setLayoutParams(params);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int titleBgColor = mContext.getResources().getColor(R.color.toast_guide_level_one);
        int normalBgColor = mContext.getResources().getColor(android.R.color.white);

        int titleTextColor = mContext.getResources().getColor(android.R.color.white);
        int normalTextColor = mContext.getResources().getColor(R.color.title_text_color);

        holder.tvSalesName.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvCount.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvAmount.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvPrice.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);
        holder.tvTotalPrice.setBackgroundColor(position == 0 ? titleBgColor : normalBgColor);

        holder.tvSalesName.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvCount.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvAmount.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvPrice.setTextColor(position == 0 ? titleTextColor : normalTextColor);
        holder.tvTotalPrice.setTextColor(position == 0 ? titleTextColor : normalTextColor);

        holder.tvSalesName.setTextSize(TypedValue.COMPLEX_UNIT_SP, position == 0 ? 18 : 16);
        holder.tvCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, position == 0 ? 18 : 16);
        holder.tvAmount.setTextSize(TypedValue.COMPLEX_UNIT_SP, position == 0 ? 18 : 16);
        holder.tvPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, position == 0 ? 18 : 16);
        holder.tvTotalPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, position == 0 ? 18 : 16);

        if (isIntegralGoods) {
            holder.tvPrice.setVisibility(View.VISIBLE);
            holder.tvTotalPrice.setVisibility(View.VISIBLE);
        } else {
            holder.tvPrice.setVisibility(View.GONE);
            holder.tvTotalPrice.setVisibility(View.GONE);
        }
        if (position == 0) {
            holder.tvSalesName.setText("商品名称");
            if(isIntegralGoods){
                holder.tvCount.setText("数量");
            }else {
                holder.tvCount.setText("销量");
            }
            holder.tvPrice.setText("单价");
            holder.tvTotalPrice.setText("金额");
            holder.tvAmount.setText(isIntegralGoods ? "积分值" : "销售总额");

            holder.tvSalesName.setPadding(DisplayUtil.dip2px(mContext, 140), 0, 0, 0);
        } else {
            SalesVolumeEntity entity = mData.get(position - 1);
            holder.tvSalesName.setText(StringUtils.get(entity.getName()));
            holder.tvCount.setText(StringUtils.getNumber(entity.getCounts()));
            if(isIntegralGoods){
                holder.tvAmount.setText(StringUtils.get(entity.getIntegral()));
            }else {
                holder.tvAmount.setText(StringUtils.getFormattedMoneyWithUnit(entity.getPrices()));
            }
            holder.tvSalesName.setPadding(DisplayUtil.dip2px(mContext, 83), 0, 0, 0);
            holder.tvPrice.setText(StringUtils.getFormattedMoneyWithUnit(entity.getPrice()));
            holder.tvTotalPrice.setText(StringUtils.getFormattedMoneyWithUnit(entity.getPrices()));
            if (position == getItemCount() - 1) {
                holder.tvSalesName.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                holder.tvSalesName.setTypeface(Typeface.DEFAULT);
            }
        }
        holder.viewLine.setVisibility(position != getItemCount() - 1 ? View.VISIBLE : View.GONE);

    }

    public void setIntegralGoods(boolean integralGoods) {
        isIntegralGoods = integralGoods;
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 1 : mData.size() + 1;
    }

    public void setData(List<SalesVolumeEntity> mData) {
        this.mData = mData;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.view_line)
        View viewLine;
        @BindView(R.id.tv_sales_name)
        TextView tvSalesName;
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.tv_amount)
        TextView tvAmount;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_total_price)
        TextView tvTotalPrice;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
